package com.ftar.urgent.util;

import android.graphics.Bitmap;
import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/**
 * @author: wei.jin
 * @date: 2023/7/10
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class FileUtils {
    public FileUtils() {
    }

    public static boolean saveBytes(byte[] data, String tofile) {
        try {
            File file = new File(tofile);
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(tofile);
            fos.write(data);
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return false;
    }

    public static boolean saveBitmap(Bitmap bitmap, String toFile) {
        return bitmap == null ? false : saveBytes(FtBitmapUtils.bitmap2bytes(bitmap), toFile);
    }

    public static byte[] loadBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedInputStream in = null;

        try {
            in = new BufferedInputStream(inputStream);
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            boolean var5 = false;

            int len;
            while(-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }

            byte[] var6 = bos.toByteArray();
            return var6;
        } catch (IOException var15) {
            var15.printStackTrace();
            throw var15;
        } finally {
            try {
                in.close();
            } catch (IOException var14) {
                var14.printStackTrace();
            }

            bos.close();
        }
    }

    public static byte[] loadBytes(String fromfile) throws IOException {
        File f = new File(fromfile);
        if (!f.exists()) {
            throw new FileNotFoundException(fromfile);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(f));
                int buf_size = 1024;
                byte[] buffer = new byte[buf_size];
                boolean var6 = false;

                int len;
                while(-1 != (len = in.read(buffer, 0, buf_size))) {
                    bos.write(buffer, 0, len);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } catch (IOException var16) {
                var16.printStackTrace();
                throw var16;
            } finally {
                try {
                    in.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }

                bos.close();
            }
        }
    }

    public static void fileDel(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }

    }

    public static void makeDir(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

    }

    public static String getPathDir(String path) {
        if (!TextUtils.isEmpty(path)) {
            String sep1 = File.separator;
            String sep2 = sep1 + sep1;
            String sep3 = sep2 + sep1;
            String sep4 = sep3 + sep1;
            if (path.contains(sep4)) {
                path = path.replace(sep4, sep1);
            }

            if (path.contains(sep3)) {
                path = path.replace(sep3, sep1);
            }

            if (path.contains(sep2)) {
                path = path.replace(sep2, sep1);
            }

            path = path.substring(0, path.lastIndexOf(sep1));
            return path;
        } else {
            return null;
        }
    }

    public static void copyFileByChannel(File source, File dest) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel destChannel = null;

        try {
            sourceChannel = (new FileInputStream(source)).getChannel();
            destChannel = (new FileOutputStream(dest)).getChannel();
            destChannel.transferFrom(sourceChannel, 0L, sourceChannel.size());
        } finally {
            if (sourceChannel != null) {
                sourceChannel.close();
            }

            if (destChannel != null) {
                destChannel.close();
            }

        }

    }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();

            for(int i = 0; i < children.length; ++i) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
}
